package com.superatom.innerclasses;


interface Selector{
    boolean end();
    Object current();
    void next();
}
public class Sequence {

    private Object[] items;
    private int next = 0;
    public Sequence(int size){
        items = new Object[size];
    }

    public void add(Object x){
        if (next < items.length){
            items[next++] = x;
        }
    }

    private class SequenceSelector implements Selector {
        private int i = 0;

        public boolean end() {
            return i == items.length;
        }

        public Object current() {
            return items[i];
        }

        public void next() {
            if (i < items.length){
                i++;
            }
        }
    }

    public Selector selector(){
        return new SequenceSelector();
    }

    // 匿名内部类
    public Selector newCreateClass(){
        //  这里是创建一个实现Selector接口的类，
        // 当然如果Selector的位置换成一个类，那就是继承一个类了
       return new Selector() {
            public boolean end() {
                return false;
            }

            public Object current() {
                return null;
            }

            public void next() {

            }
        };
    }

    public static void main(String[] args){
        Sequence sequence = new Sequence(10);
        for (int i = 0; i < 10; i++){
            sequence.add(Integer.toString(i));
        }

        Selector selector = sequence.selector();
        while (!selector.end()){
            System.out.print(selector.current() + " ");
            selector.next();
        }
    }
}
